# -*- coding: utf-8 -*-
# @Time    : 2020/6/4 9:40 上午
# @Author  : harumonia
# @FileName: outline.py
# @Software: PyCharm
# @Description: None
from flask_admin.contrib.sqla import ModelView
from flask_login import current_user

from application.extensions import db


class Outline(db.Model):
    __tablename__ = 'outline'
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='id')
    courseId = db.Column(db.BigInteger, db.ForeignKey('course.id'))
    outlineName = db.Column(db.String(40), comment='大纲名字')
    outlineRank = db.Column(db.Float, comment='大纲排序')
    isHeader = db.Column(db.Boolean, comment='是否为父级节点')
    remark = db.Column(db.String(255), comment='大纲说明')
    course = db.relationship('Course',
                             backref=db.backref('outline'))

    def __repr__(self):
        return str(self.id) + '. ' + self.outlineName


class OutlineView(ModelView):
    form_excluded_columns = ['article']
    def on_model_change(self, form, Outline, is_created):
        Outline.courseId = current_user.id

    def on_model_delete(self, Article):
        Article.status = False
